691C - Exponential notation - CodeForces Solution


implementation strings *1800

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define speedup ios_base::sync_with_stdio(false);cin.tie(NULL);
#define getUnique(v) {sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end());}
const int mod=1e9+7;
double binexp(double n,ll p){if(p==0)return 1;double res=binexp(n,p/2);if(p&1) return res*res*n;return res*res;}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
 
void solve(){
    string s;
    cin>>s;
    int i=0;
    // bool vis=0;
    int cnt=0;
    
    if((int)s.size()==1 && s[0]=='0'){
        cout<<0;
        return;
    }
    vector<int>a,b;
    if(s[0]=='0'){
        while(s[i]=='0'){
            i++;
        }
    }
    if(find(s.begin(),s.end(),'.')==s.end()){
        int y=s.size()-1;
        int cnt=0;
        while(s[y]=='0')y--,cnt++;
        if((int)s.size()-i-cnt==1){
            cout<<s[y];
            if(cnt!=0)
            cout<<"E"<<cnt;
            return;
        }
        else{
            int j=i;
            cout<<s[i]<<".";
            i++;
            for(;i<=y;i++)cout<<s[i];
            if((int)s.size()-1!=0)
            cout<<"E"<<(int)s.size()-1-j;
            return;
        }
        
    }
    if(s[i]!='.')
    while(s[i]!='.'){a.pb(s[i]-'0');i++;cnt++;}
    i++;
    int j=(int)s.size()-1;
        if(s[j]=='0')
        while(s[j]=='0')
            j--;
        
    for(;i<=j;i++)b.pb(s[i]-'0');
    // for(auto x:a)cout<<x;
    //     cout<<"\n";
    // for(auto x:b)cout<<x;
    if((int)a.size()==0){
        if(b.size()==0){
            cout<<0;return;
        }
        int y=0;
        while(b[y]==0)y++;
        if((int)b.size()-y==1){
            cout<<b[y]<<"E";
            cout<<-(y+1);return;
        }else{
            cout<<b[y];
            cout<<".";
            for(int i=y+1;i<b.size();i++)cout<<b[i];
            cout<<"E"<<-(y+1);
        }
        
    }else{
        if((int)b.size()==0){
            int cnt=0;
            int y=a.size()-1;
            while(a[y]==0 && y>=0)cnt++,y--;
            //cout<<b.size();
            
            if((int)a.size()-cnt==1){
                cout<<a[0];
                if(cnt!=0){
                cout<<"E";
                cout<<(cnt);
                }
            }else{
                cout<<a[0];
                cout<<".";
                for(int i=1;i<=y;i++){
                    cout<<a[i];
                }
                if((int)a.size()-1!=0){
                cout<<"E";cout<<(int)a.size()-1;
                }
            }
        }else{
            cout<<a[0];
            cout<<".";
            for(int i=1;i<a.size();i++)cout<<a[i];
            for(auto x:b)cout<<x;
            if((int)a.size()-1!=0){
            cout<<"E";
            cout<<(int)a.size()-1;
            }
        }
    }
    
}
int main(){
    speedup;
    int t;t=1;
    // cin>>t;
    while(t--)solve();
}


Comments

Submit
0 Comments
More Questions

822A - I'm bored with life
9A - Die Roll
1430B - Barrels
279B - Books
1374B - Multiply by 2 divide by 6
1093B - Letters Rearranging
1213C - Book Reading
1468C - Berpizza
1546B - AquaMoon and Stolen String
1353C - Board Moves
902A - Visiting a Friend
299B - Ksusha the Squirrel
1647D - Madoka and the Best School in Russia
1208A - XORinacci
1539B - Love Song
22B - Bargaining Table
1490B - Balanced Remainders
264A - Escape from Stones
1506A - Strange Table
456A - Laptops
855B - Marvolo Gaunt's Ring
1454A - Special Permutation
1359A - Berland Poker
459A - Pashmak and Garden
1327B - Princesses and Princes
1450F - The Struggling Contestant
1399B - Gifts Fixing
1138A - Sushi for Two
982C - Cut 'em all
931A - Friends Meeting